package com.comunity_cloud.client.view2;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

import com.comunity_cloud.client.model.CommunityFile;
import com.comunity_cloud.client.model.CommunityModel2;

public class FileTableCellRenderer implements TableCellRenderer{

	private Map<CommunityFile, JLabel> labels = new HashMap<CommunityFile, JLabel>();
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		
		CommunityModel2 model = (CommunityModel2) table.getModel();
		CommunityFile file = model.getRow(row);
		
		JLabel label = labels.get(file);
		if(label == null) {
			 label = new JLabel();
			 labels.put(file, label);
		}
		label.setBackground(table.getBackground());
		label.setText(value.toString());
		label.setBorder(new EmptyBorder(0, 5, 0, 5));
		label.setOpaque(true);
		if(isSelected){
			label.setForeground(table.getSelectionForeground());
			label.setBackground(table.getSelectionBackground());
		} else {
			label.setForeground(table.getForeground());
			label.setBackground(table.getBackground());
		}
		switch (column) {
		case 0:
			if (label.getIcon() == null && file.getImage() != null) {
				label.setIcon(new ImageIcon(file.getImage()));
			}
			break;
		default:
			break;
		}
		return label;
	}
	
	

}
